<?php

    class iCms extends CI_Model {

        function __construct() {
            parent::__construct();
        }

        public function categories($IDS = NULL) {
            $this->db->where('parent_ID', 0);

            if (isset($IDS))
                $this->db->where_not_in('ID', $IDS);

            $this->db->order_by("sort", "asc");
            $res = $this->db->get('category')->result_array();
            return $res;
        }

        public function find_by_cat($cat_id, $order = NULL, $limit = NULL, $parent = NULL) {
            if ($cat_id == NULL)
                return NULL;

            $this->db->where('category_ID', $cat_id);

            if (isset($parent))
                $this->db->where('parent_ID', $parent);

            //find category sort
            $SORT_ORDER = find_sort_type($this->db, $cat_id);
            $this->db->order_by('sort', $SORT_ORDER);
            $this->db->order_by('ID', $SORT_ORDER);

            if (isset($order)):
                $this->db->order_by('sort', 'ASC');
                $this->db->order_by('ID', 'DESC');
            endif;

            if (isset($limit))
                $this->db->limit($limit);

            $this->db->group_by('ID');
            $query = $this->db->get("post_pic");

            if (isset($limit) && $limit == 1)
                $result = $query->row_array();
            else
                $result = $query->result_array();

            return (count($result) > 0 ? $result : NULL);
        }

        public function find_cat_id($id) {
            if ($id == NULL) {
                return NULL;
            }

            $this->db->where('ID', $id);
            $query = $this->db->get('category');

            $result = $query->result_array();
            return (count($result) > 0 ? $result[0] : NULL);
        }

        public function find_post_id($id, $order = NULL) {
            if ($id == NULL) {
                return NULL;
            }

            $this->db->where('ID', $id);

            if (isset($order))
                $this->db->order_by('photo_order', 'ASC');

            $this->db->group_by('ID');

            $query = $this->db->get('post_pic');

            $result = $query->result_array();
            return (count($result) > 0 ? $result[0] : NULL);
        }

        public function find_files($post_ID) {

            $sql = "SELECT * FROM file_folder WHERE post_ID = ? ";

            $res = $this->db->query($sql, array($post_ID));

            return $res->result_array();
        }

        public function find_photos($post_ID) {

            $sql = "SELECT * FROM pic_gallery WHERE post_ID = ? ORDER BY sort ASC";

            $res = $this->db->query($sql, array($post_ID));

            return $res->result_array();
        }

        public function find_top_photo($post_ID) {

            $sql = "SELECT * FROM pic_gallery WHERE post_ID = ? ORDER BY sort ASC";

            $res = $this->db->query($sql, array($post_ID));

            return $res->row_array();
        }

        function get_attributes_on_group($group_id, $custom = NULL, $active = TRUE, $required = NULL) {
            $q = "
            SELECT 
            group_has_attribute.attribute_ID as attribute_ID,
            attribute.name as attribute_name,
            attribute.type as attribute_type,
            attribute.required as attribute_required,
            attribute.custom as attribute_custom,
            attribute.sort as attribute_sort
            FROM group_has_attribute
            LEFT JOIN attribute ON group_has_attribute.attribute_ID = attribute.ID
            WHERE group_has_attribute.group_ID = $group_id
            AND group_has_attribute.status = $active
            ";

            if (isset($custom))
                $q.=" AND attribute.custom = 1";
            if (isset($required))
                $q.=" AND attribute.required = 1";

            $q.= " ORDER BY group_has_attribute.sort ASC";

            $res = $this->db->query($q)->result_array();
            return $res;
        }

        function find_group_id($category_ID) {
            $this->db->where('category_ID', $category_ID);
            $res = $this->db->get('category_has_group')->row_array();
            return isset($res['group_ID']) ? $res['group_ID'] : -1;
        }

        function find_child_post($parent_ID, $cat_id, $order = NULL, $limit = NULL) {

            if ($parent_ID == NULL)
                return NULL;

            $this->db->where('parent_ID', $parent_ID);

            //find category sort
            $SORT_ORDER = find_sort_type($this->db, $cat_id);
            $this->db->order_by('sort', $SORT_ORDER);
            $this->db->order_by('ID', $SORT_ORDER);

            if (isset($order)):
                $this->db->order_by('sort', 'ASC');
                $this->db->order_by('ID', 'DESC');
            endif;

            if (isset($limit))
                $this->db->limit($limit);

            $this->db->group_by('ID');
            $query = $this->db->get("post_pic");

            if (isset($limit) && $limit == 1)
                $result = $query->row_array();
            else
                $result = $query->result_array();

            return (count($result) > 0 ? $result : NULL);
        }

    }
    